docs.admin = liwe.module ( 'docs.admin' );

docs.admin._events.tree_move_up = function ( tree, evt_name, div, node )
{
	docs.admin._move_node ( evt_name, node.id ); 
	console.debug ( "Tree: %o - evt: %s - div: %o - node: %o", tree, evt_name, div, node );
};
docs.admin._events.tree_move_down = function ( tree, div , node )
{
	docs.admin._move_node ( evt_name, node.id ); 
	console.debug ( "Tree: %o - evt: %s - div: %o - node: %o", tree, evt_name, div, node );
};

docs.admin.init = function ( dest_div )
{
	var _acts = [
                        { func: function () { docs.dest_div = 'system-content' ; docs.admin.render (); }, img_src: '', label: 'Docs', "class" : "docs" }
		];
	docs.admin.register ( _acts );
};

docs.admin.render = function ( dest_div )
{
	if ( ! dest_div ) dest_div = docs.dest_div;
	docs.render ( dest_div );	
	if ( ! docs.admin._tabs ) docs.admin._tabs_init ();
	docs.admin._create_editor ();
	docs.tree.show_checkbox = true;
};

docs.admin.add_node = function ()
{
	docs.admin._set_editor_data ( 0, "", "" );
	if ( docs.admin._tabs ) docs.admin._tabs.send_event ( "DOCS-tabs-editor", "click" );
};

docs.admin.delete_node = function ()
{
	var node_lst = docs.tree.get_selection ();
	var node_ids = [];
	// clean array
	node_lst.iterate ( function ( v, k ) 
				{
					if ( parseInt ( v, 10 ) ) node_ids.push ( v );
				} );
	//var curr_node = docs.tree.current_get ();
	var conf = false;
	conf = confirm ( "Vuoi eliminare il/i  nodo/i selezionati ? " );
	if ( conf ) {
		docs.admin.ajax ( { action : 'docs.admin.del_node', id_node : node_ids.join ( '|' ) } , function ( data ) 
									{
										docs.admin.render ();
									} );
	}
};

docs.admin._move_node = function ( evt_name, node )
{
	docs.admin.ajax ( { action : "docs.admin.move_node", evt_name : evt_name, id_node : node }, function ( data ) {} );
};

docs.admin._add_node = function ()
{
	var f = liwe.form.get ( "docs_text_form" );
	var title = f.get_value ( "title" );
	var txt = f.get_value ( 'text' );
	var id_node = f.get_value ( 'id_node' );

	docs.ajax ( { action: "docs.admin.add_node",
		  parent_id: ( docs._curr_node ? docs._curr_node.id : 0 ),
		  title: title,
		  text: txt,
		  id_node: id_node
		}, function ( v ) {
			console.debug ( v );
		} );
	docs.admin.render ();
};
docs.admin._tabs_init = function ()
{
	if ( ! $( "DOCS-tabs" ) ) return;

	docs.admin._tabs = new OS3Tabs.instance ( 'tabs' );

	docs.admin._tabs.add ( 'Main', 'DOCS-tabs-preview' );
	docs.admin._tabs.add ( 'Edit', 'DOCS-tabs-editor' );

	docs.admin._tabs.render ( 'DOCS-tabs' );
};

docs.admin._create_editor = function ()
{
	if ( ! docs.admin._tabs ) return;

	var f = new liwe.form.instance ( 'docs_text_form' );

	f.text ( { label: "Titolo", name: "title", size: 40, mandatory: true } );
	f.htmled ( { name: "text", rows: 60, cols: 300 } );
	f.button ( { value: "Invia", onclick: "docs.admin._add_node()" } );
	f.hidden ( "id_node", 0 );

	f.set ( 'DOCS-tabs-editor' );
};

docs.admin._set_editor_data = function ( id_node, title, body )
{
	var f = liwe.form.get ( 'docs_text_form' );

	if ( ! f ) return false;
	f.set_value ( 'title', title );
	f.set_value ( 'text', body );
	f.set_value ( 'id_node', id_node );

	return true;
};

